<template>
  <div>
    <el-form ref="modelForm" size="small" :model="entity" :rules="rules"  @submit.native.prevent label-width="120px">
      <el-form-item label="ModelName" prop="modelname">
        <el-input v-model="entity.modelname" @keyup.enter.native="onSubmit" ref="modelname" placeholder="ModelName" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" size="mini" @click="addModelStep" style="float: right">add step</el-button>
      </el-form-item>
      <el-form-item label="step title" prop="stepname" v-for="(item,index) in modelStepList" :key="index + '-' + item.id">
        <el-input v-model="item.stepname" placeholder="step title" >
          <template slot="append">
            <el-popconfirm confirmButtonText='ok' cancelButtonText='cancel'
                           icon="el-icon-info" iconColor="red"
                           title="confirm delete?" @confirm="deleteModelStep(item.id, index)">
              <el-button slot="reference" type="danger" icon="el-icon-delete"></el-button>
            </el-popconfirm>
          </template>
        </el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" size="mini" @click="onSubmit">ok</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import request from '@/utils/request'
export default {
  props:{
    entityId : {type : Number, default: 0}
  },
  data() {
    return {
      entity: {
        modelname: '',
      },
      modelStepList:[],
      rules: {
        modelname: [
          { required: true, message: 'please input ModelName', trigger: 'blur' }
        ],
      }
    }
  },
  mounted() {
    this.loadEntity();
    this.loadModelStep();
    this.$nextTick(()=>{
      this.$refs.modelname.focus()
    })
  },
  methods: {
    loadEntity(){
      if(this.entityId > 0){
        request({ url: '/emp/stepmodel/getmodel', data: {id : this.entityId} }).then( res =>{
          this.entity = res.data
        })
      }
    },
    onSubmit(){
      this.entity.modelmxList = this.modelStepList
      this.$refs['modelForm'].validate((valid) => {
        if(valid){
          request({
            url: '/emp/stepmodel/addeditmodel',
            data: this.entity
          }).then( res =>{
            if(res.code === "200"){
              this.$message.success(res.msg)
              this.$emit('update:visible', false);  //关闭弹窗
              //触发事件总线，让列表界面更新数据
              this.entityId > 0 ? this.$EventBus.$emit("modeledit", {}):this.$EventBus.$emit("modeladd", {})
            }else{
              this.$message.error(res.msg);
            }
          })
        }else{
          console.log('error submit!!');
          return false;
        }
      })
    },
    /*模板步骤*/
    loadModelStep(){
      if(this.entityId > 0){
        request({ url: '/emp/stepmodel/listmodelmx', data: {id:this.entityId} }).then( res =>{
          this.modelStepList = res.data
          if(this.modelStepList.length === 0){
            this.modelStepList.push({stepname:'produce step', sortid:0, modelid:this.entityId, id:0})
          }
        })
      }else{
        this.modelStepList.push({stepname:'produce step', sortid:0, modelid:this.entityId, id:0})
      }
    },
    addModelStep(){
      let sortIdArray = this.modelStepList.map((val) => val.sortid)
      sortIdArray.sort((a,b)=>a-b);
      const maxSortId = sortIdArray[sortIdArray.length -1];
      this.modelStepList.push({stepname:'produce step', sortid:(maxSortId + 100), modelid:this.entityId, id:0})
    },
    deleteModelStep(id, index){
      if(id>0){
        request({ url: '/emp/stepmodel/deletemodelmx', data: {id:id} }).then( res =>{
          this.modelStepList = this.modelStepList.filter((s,i)=>i!==index)
        })
      }else{
        this.modelStepList = this.modelStepList.filter((s,i)=>i!==index)
      }
    },
  }
}
</script>

<style scoped>

</style>
